package com.tuxing.draa_data.service.metadata;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.tuxing.draa_data.dao.center.NewDcBibliographyPeriDao;
import com.tuxing.draa_data.dao.center.NewDcBibliographyPeriDetailDao;
import com.tuxing.draa_data.dto.CnJournalExcelDto;
import com.tuxing.draa_data.pojo.center.NewDcBibliographyPeri;
import com.tuxing.draa_data.pojo.center.NewDcBibliographyPeriDetail;
import com.tuxing.draa_data.util.CheckUtil;
import com.tuxing.draa_data.util.FormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yuanshihang
 * @version 1.0
 * @className MetaDataServiceImpl
 * @description 基础描述:元数据解析类实现
 * @date 2022/1/4 19:21
 */
@Service
@Slf4j
public class MetaDataServiceImpl implements MetaDataService {

    @Resource
    private NewDcBibliographyPeriDao periDao;

    @Resource
    private NewDcBibliographyPeriDetailDao detailDao;


    /**
     * @param cnJournalList 中文期刊的Excel数据
     * @return 暂无返回结果
     * @decpription 进行中文期刊的解析和保存
     * @author yuanshihang
     * @date 2022/1/4 19:26
     * @version 1.0
     */
    @Override
    public void saveCnJournalData(List<CnJournalExcelDto> cnJournalList) {
        if (CollectionUtils.isEmpty(cnJournalList)) {
            return;
        }
        for (CnJournalExcelDto cnJournalExcelDto : cnJournalList) {
            //格式化ISSN号
            String issn = FormatUtil.formatISSN(cnJournalExcelDto.getIssn());
            //工具类期刊检索
            NewDcBibliographyPeri oneJournal = CheckUtil.findOneJournal(cnJournalExcelDto.getTitle(),
                    issn, null, cnJournalExcelDto.getCn(), periDao);
            //没有期刊信息就进行新增，有期刊信息就进行更新
            if (oneJournal == null) {
                oneJournal = new NewDcBibliographyPeri();
                oneJournal.createByCnki(cnJournalExcelDto);
                periDao.insert(oneJournal);
                Long recordId = oneJournal.getRecordId();

                NewDcBibliographyPeriDetail periDetail = new NewDcBibliographyPeriDetail();
                periDetail.createByCnki(recordId, cnJournalExcelDto);
                detailDao.insert(periDetail);
                log.info("新增期刊：" + JSON.toJSONString(oneJournal));
            } else {

                //更新操作
                oneJournal.createByCnki(cnJournalExcelDto);
                periDao.updateById(oneJournal);
            }
        }
    }

    @Override
    public void addMetaJournalDataFromCh() {
        QueryWrapper<NewDcBibliographyPeri> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SOURCE", 2).eq("LANG_CODE", 1);
        queryWrapper.last("and P_ISSN is not null");
        List<NewDcBibliographyPeri> periList = periDao.selectList(queryWrapper);
        for (NewDcBibliographyPeri peri : periList) {
            String pIssn = peri.getPIssn();
            String oIssn = peri.getOIssn();
            String cn = peri.getPeriCnNo();
            String title = peri.getTitle();
            Integer langCode = peri.getLangCode();

//            NewDcBibliographyPeri sameOne = new NewDcBibliographyPeri();
            if (!Strings.isNullOrEmpty(pIssn)) {
                queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("LANG_CODE", langCode).eq("SOURCE", 0).eq("P_ISSN", pIssn);
                queryWrapper.last("limit 1");
                NewDcBibliographyPeri sameOne = periDao.selectOne(queryWrapper);
                if (sameOne == null) {
                    queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("LANG_CODE", langCode).eq("SOURCE", 0).eq("O_ISSN", pIssn);
                    queryWrapper.last("limit 1");
                    sameOne = periDao.selectOne(queryWrapper);
                    if (sameOne != null) {
                        log.info("-----------eissn有比对:" + pIssn);
                        continue;
                    }
                } else {
                    log.info("pissn有比对");
                    continue;
                }
            }

//            if (!Strings.isNullOrEmpty(oIssn) || !Strings.isNullOrEmpty(cn)) {
//
//                queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("LANG_CODE", langCode).eq("SOURCE", 0);
//                if (langCode == 1) {
//                    queryWrapper.eq("O_ISSN", oIssn);
//                } else {
//                    queryWrapper.eq("PERI_CN_NO", cn);
//                }
//                queryWrapper.last("limit 1");
//                NewDcBibliographyPeri sameOne = periDao.selectOne(queryWrapper);
//                if (sameOne != null) {
//                    continue;
//                }
//            }
            if (!Strings.isNullOrEmpty(title)) {
                queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("LANG_CODE", langCode).eq("SOURCE", 0).eq("TITLE", title);
                queryWrapper.last("limit 1");
                NewDcBibliographyPeri oneByTitle = periDao.selectOne(queryWrapper);
                if (oneByTitle == null) {
                    log.info("---------------新增期刊-----------------");
                    NewDcBibliographyPeri newOne = new NewDcBibliographyPeri();
                    BeanUtils.copyProperties(peri, newOne);
                    newOne.setRecordId(null);
                    newOne.setSource(0);
                    newOne.setCreateBy(-2);
                    newOne.setUpdateBy(-2);
                    newOne.setCreateDate(LocalDateTime.now());
                    newOne.setUpdateDate(LocalDateTime.now());
                    periDao.insert(newOne);


                    NewDcBibliographyPeriDetail periDetail = detailDao.selectById(peri.getRecordId());
                    if (periDetail != null) {
                        NewDcBibliographyPeriDetail newDetail = new NewDcBibliographyPeriDetail();
                        BeanUtils.copyProperties(periDetail, newDetail);
                        newDetail.setRecordId(newOne.getRecordId());
                        detailDao.insert(newDetail);
                    }
                }

            }
        }
    }
}
